package com.easy;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created by Lxk on 2019/6/8.
 */
public class Solution429 {

    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root==null){
            return res;
        }
        Node pre = root,last = root;
        List<Integer> temp = new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            Node cur = queue.poll();
            temp.add(cur.val);
            for(Node node : cur.children){
                queue.offer(node);
                last = node;
            }
            if(cur==pre){
                res.add(temp);
                temp = new ArrayList<>();
                pre = last;
            }
        }
        return res;
    }

}
